const express = require("express");
const exApp = require("../index");
const router = express.Router();
const mysql = require("../connect/pomsql");
const { SuccessModule, ErrorModule } = require("../model/resModel");
const { resHandler } = require("../model/resHandler.js");
const path = require("path");
const multer = require("multer");

exApp.get("/getlist", async function (req, res) {
  let sql = `SELECT * FROM lxfxcx`;
  let data = await mysql(sql);
  res.json({
    code: 200,
    data,
  });
});

exApp.post("/likes", async function (req, res) {
  let { like, id } = req.body;
  let sql = `UPDATE lxfxcx SET likes = '${like}' WHERE id = '${id}'`;
  let data = await mysql(sql);
  res.json({
    code: 1,
    data,
  });
});
exApp.post("/comment", async function (req, res) {
  let { comment, id } = req.body;
  console.log(comment);
  let sql = `UPDATE lxfxcx SET comment =  '${comment}' WHERE id = '${id}'`;
  let data = await mysql(sql);
  res.json({
    code: 1,
    data,
  });
});
exApp.post("/focus", async function (req, res) {
  let { focus, name } = req.body;
  let sqll = `SELECT * FROM user  WHERE  username  ='${name}'`;
  let list = await mysql(sqll);
  let foc = list[0].focus?JSON.parse(list[0].focus):[];
  foc.push(focus)
  let sql = `UPDATE user SET focus =  '${JSON.stringify(foc)}' WHERE username = '${name}'`;
  let data = await mysql(sql);
  res.json({
    code: 1,
    data,
  });
});
exApp.post("/fans", async function (req, res) {
  let { fans, name } = req.body;
  let sqll = `SELECT * FROM user  WHERE  username  ='${name}'`;
  let list = await mysql(sqll);
  let fan = list[0].fans?JSON.parse(list[0].fans):[];
  fan.push(fans)
  let sql = `UPDATE user SET fans =  '${JSON.stringify(fan)}' WHERE username = '${name}'`;
  let data = await mysql(sql);
  res.json({
    code: 1,
    data,
  });
});
exApp.post("/focus", async function (req, res) {
  let { focus, name } = req.body;
  let sql = `UPDATE user SET focus =  '${focus}' WHERE username = '${name}'`;
  let data = await mysql(sql);
  res.json({
    code: 1,
    data,
  });
});
exApp.post("/getuser", async function (req, res) {
  let { name } = req.body;
  let sql = `SELECT * FROM user  WHERE  username  ='${name}'`;
  let data = await mysql(sql);
  res.json({
    code: 1,
    data,
  });
});
exApp.post("/focususer", async function (req, res) {
  let { word } = req.body;
  console.log(req.body);
  let sql = `SELECT * FROM lxfxcx  WHERE userid LIKE '%${word}%'`;
  let data = await mysql(sql);
  res.json({
    code: 1,
    data,
  });
});

// 开放静态图片端口
exApp.use(express.static(path.resolve("./public")));
